<?php

namespace App\Http\Controllers\Mapi;
use App\Http\Controllers\Controller;
use App\Http\Validators\MusicValidator;
use App\Services\MusicService;
use Illuminate\Http\Request;
use App\Api\Helper\ReturnMessage;

class MusicController extends Controller
{

    /**
     * @param Request $request
     * @return \Dingo\Api\Http\Response
     * @name 音乐作品 删除
     * @user IHammer
     */
    public function MusicDelete(Request $request){
        $errmsg = "操作失败";
        $request->offsetSet("user_id",constant('_USER_ID'));
        $res = MusicService::MusicDeleteHandle($request,$errmsg);
        if($res){
            return ReturnMessage::success('操作成功');
        }
        return ReturnMessage::success($errmsg,1001);
    }
    /**
     * @param Request $request
     * @return \Dingo\Api\Http\Response
     * @name  音乐作品详情
     * @user IHammer
     */
    public function MusicInfo(Request $request){
        $errmsg = "操作失败";
        $id = $request->get('id');
        $lists = MusicService::MusicInfoHandle($id,$errmsg);
        if($lists){
            return ReturnMessage::successArray($lists);
        }else{
            return ReturnMessage::success($errmsg,1001);
        }

    }

    /**
     * @param Request $request
     * @return \Dingo\Api\Http\Response
     * @name 音乐作品列表
     * @user IHammer
     */
    public function MusicLists(Request $request){
        $input = $request->input();
        $pageSize = empty($input['pageSize']) ? 10 : $input['pageSize'];
        if(!empty($input['showMine'])){
            $input['user_id'] = _USER_ID;
        }
        $lists = MusicService::MusicListsHandle($input,$pageSize);
        return ReturnMessage::successLists($lists);
    }

    /**
     * @param Request $request
     * @return \Dingo\Api\Http\Response
     * @name 新增音乐作品
     * @user IHammer
     */
    public function MusicCreate(Request $request){
        MusicValidator::create($request);
        $errmsg = "操作失败";
        $request->offsetSet("user_id",constant('_USER_ID'));
        $res = MusicService::MusicCreateHandle($request,$errmsg);
        if($res){
            return ReturnMessage::success('操作成功');
        }
        return ReturnMessage::success($errmsg,1001);
    }

    public function MusicEdit(Request $request){
        MusicValidator::create($request);
        $errmsg = "操作失败";
        $request->offsetSet("user_id",constant('_USER_ID'));
        $res = MusicService::MusicEditHandle($request,$errmsg);
        if($res){
            return ReturnMessage::success('操作成功');
        }
        return ReturnMessage::success($errmsg,1001);
    }

    /**
     * @param Request $request
     * @return \Dingo\Api\Http\Response
     * @name 分享记录
     * @user IHammer
     */
    public function MusicShareAdd(Request $request){
        $errmsg = "操作失败";
        $res = MusicService::MusicShareAddHandle($request,$errmsg);
        if($res){
            return ReturnMessage::success('操作成功');
        }
        return ReturnMessage::success($errmsg,1001);
    }
}
